package com.winit.generator.task;

import com.winit.generator.config.Configuration;
import com.winit.generator.framework.AbstractApplicationTask;
import com.winit.generator.framework.context.ApplicationContext;
import com.winit.generator.handler.BaseHandler;
import com.winit.generator.handler.impl.MapperHandler;
import com.winit.generator.model.EntityInfo;
import com.winit.generator.model.MapperInfo;

import java.util.List;
import java.util.Map;

public class MapperTask extends AbstractApplicationTask {
    @SuppressWarnings("unchecked")
    @Override
    protected boolean doInternal(ApplicationContext context) {
        String MAPPER_FTL = "";
        if ("configuration_mrp.xml".equals(Configuration.CONFIG_FILE_NAME)) {
            MAPPER_FTL = "template/Mapper_mrp_member_id.ftl";
        } else if ("configuration_jei_member.xml".equals(Configuration.CONFIG_FILE_NAME)) {
            MAPPER_FTL = "template/Mapper_mrp_member.ftl";
        } else {
            MAPPER_FTL = "template/Mapper_member_id.ftl";
        }


        logger.info("开始生成Mapper");

        List<MapperInfo> list = (List<MapperInfo>) context.getAttribute("mapperInfos");

        BaseHandler<MapperInfo> handler = null;
        for (MapperInfo mapperInfo : list) {
            EntityInfo entityInfo = mapperInfo.getEntityInfo();
            Map<String, String> propTypes = entityInfo.getPropTypes();
            if (!propTypes.containsKey("isDeleted")) {

                String substring = MAPPER_FTL.substring(0, MAPPER_FTL.lastIndexOf("."));

                MAPPER_FTL = substring + "_noIsDeleted.ftl";
            }

            handler = new MapperHandler(MAPPER_FTL, mapperInfo);
            handler.execute(context);
        }

        logger.info("生成Mapper完成");
        return false;
    }

    @Override
    protected void doAfter(ApplicationContext context) throws Exception {
        super.doAfter(context);
    }
}
